﻿using System;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Media;
using ThePresenter.Net.PresenterCom.Uids;

namespace ThePresenter.Net.PresenterCom.Actions
{
	[Serializable]
	class ManipulationAction : CanvasAction
	{
		UidCollection _Selection;
		Rect _NewRect;

		public ManipulationAction(StrokeCollection Selection, Rect NewRect, Rect OldRect)
		{
			this._Selection = new UidCollection(Selection);
			this._NewRect = NewRect;
		}

		public override void Execute(InkCanvas Target)
		{
			UidStrokesCollection IDStrokes = Target.Tag as UidStrokesCollection;

			StrokeCollection Selection = this._Selection.FilterStrokes(IDStrokes);
			Rect OldRect = Selection.GetBounds();
			Matrix m = GetTransformFromRectToRect(OldRect, _NewRect);
			Selection.Transform(m, false);
		}

		static Matrix GetTransformFromRectToRect(Rect src, Rect dst)
		{
			Matrix m = Matrix.Identity;
			m.Translate(-src.X, -src.Y);
			m.Scale(dst.Width / src.Width, dst.Height / src.Height);
			m.Translate(+dst.X, +dst.Y);
			return m;
		}
	}
}


